var cf = require("../../../config.js");
var util = require("../../../utils/util.js");
var cusmallToken = wx.getStorageSync('cusmallToken');
var baseHandle = require("../../template/baseHandle.js");
//获取应用实例
var app = getApp();
Page(Object.assign({}, baseHandle, {

  /**
   * 页面的初始数据
   */
  data: {
	  extConfig: wx.getExtConfigSync ? wx.getExtConfigSync() : {},
	  app: app,
	  staticResPath: cf.config.staticResPath,
	  userImagePath: cf.config.userImagePath,
	  welwordDesc: "",
	  quickEnter: 127,
	  employee: "",
	  companyInfo: "",
	  isLoading: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
	  wx.hideShareMenu();
	  var that = this;
	  that.setData({isLoading:true});
      wx.showLoading({title: '加载中...'});
	  app.getUserInfo(this, options, function (userInfo, res) {
	      cusmallToken = wx.getStorageSync('cusmallToken');
		  that.data.companyInfo = wx.getStorageSync("companyInfo");
		  that.fetchData();
	  });
  },
  
  fetchData: function () {
	  var that = this;
	  wx.request({
	      url: cf.config.pageDomain + "/applet/mobile/visitcard/employee/getEmployeeInfo",
	      data: {
	        cusmallToken: cusmallToken,
	      },
	      header: {
	        'content-type': 'application/json'
	      },
	      success: function (res) {
	    	wx.hideLoading();
        let info = that.data.employee
	    	var data = res.data;
	        if (data && 0 === data.ret) {
	          var employee = data.model.employee;
            var project = employee.salesProductName || employee.projectInfo ? JSON.parse(employee.projectInfo)[0].projectName : that.data.companyInfo.company
            console.log(project)
            var wordDesc = employee.welcomeSpeech || "您好，我是" + project + "的" + employee.name+"，欢迎进入我的"+app.globalData.cardTitle+"，有什么可以帮到你的吗?你可以在这里跟我实时沟通。";
	          var qukEn = employee.quickEntrance==-1?that.data.quickEnter:employee.quickEntrance;
	          that.setData({
	             employee:employee,
	             welwordDesc:wordDesc,
	             quickEnter: qukEn
	          });
	        } else {}
	      },
	      fail() {
	    	  wx.hideLoading();
	      },
	      complete() {
	    	  that.setData({isLoading:false});
	      }
	 });  
  },
  
  welwordInput: function (e) {
	  this.setData({
		  welwordDesc: e.detail.value
	  });
  },
  
  qukEnChange: function (e) {
	  var ctx = this;
	  var idx = e.currentTarget.dataset.idx;
	  var val = e.detail.value;
	  ctx.setData({
		  quickEnter: Math.pow(2, idx)*val+ctx.data.quickEnter
	  });
  },
  
  welwordCommit: function () {
		var ctx = this;
		let employee = wx.getStorageSync('employee')
    //收集用户保存欢迎语
    app.globalData.sensors.track('1008', {
      account_id: wx.getStorageSync('userId'),
      role: employee ? '置业顾问' : '用户'
    });
	  if (0 === ctx.data.welwordDesc.length){
	      wx.showToast({
	        title: '欢迎语不能为空',
	        icon:"none"
	      });
	      return;
	  }
	  if (ctx.data.isLoading){
	      wx.showToast({
	        title: '不要重复提交',
	        icon: "none"
	      });
	      return;
	  }
	  ctx.setData({isLoading:true});
	  wx.showLoading({
	      title: '提交中'
	  });
	  wx.request({
		 url: cf.config.pageDomain + "/applet/mobile/visitcard/employee/modifyEmployeeInfo",
		 data: {
			cusmallToken: cusmallToken,
	        id: ctx.data.employee.id,
	        welcomeSpeech: ctx.data.welwordDesc,
	        quickEntrance: ctx.data.quickEnter
		 },
		 header: {
	        'content-type': 'application/json'
	     },
	     success: function (res) {
	         var data = res.data;
	         wx.hideLoading()
	         if (data && 0 === data.ret) {
	           wx.showModal({
	             title: '提示',
	             content: '保存成功',
	             showCancel: false,
	             success(){
	               setTimeout(function(){
	                 wx.navigateBack({
	                   delta: 1
	                 });
	               },500);
	             }
	           });
	         }else{
	 	       wx.showModal({
	             title: '提示',
	             content: '保存失败',
	             showCancel: false
	           })
	         }
	       },
	       fail() {
	         wx.hideLoading()
	       },
	       complete() {
	         ctx.setData({
	           isLoading: false
	         });
	       }
	   });
  },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
	  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
}))